package com.whitetiger.filer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 文件处理
 */
public class Filer {
	/**
	 * 读取文件内容为字符串
	 * 
	 * @param path 文件物理路径
	 * @param encoding 读取文件的编码 
	 * eg: utf-8
	 * @throws IOException 读取文件异常
	 */
	public static final String ReadFileAsString(String path, String encoding) throws IOException {
		String strResult = "";
		try {
			File f = new File(path);
			if (f.exists()) {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), encoding));
				String line = null;
				while ((line = reader.readLine()) != null) {
					strResult += line + "\n";
				}
			}else{
				strResult="file not found";
			}
		} catch (IOException e) {//file read error: used by other thread and so on.
			throw e;
		}
		return strResult;
	}
}
